
using System;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_ORDER.DBAccess
{

	public class ProductDAO : BaseDAO
	{
        #region Basic Method

        public static DataTable GetAll()
        {
            return ExcuteProcedure(OrderConstants.StoreProcedure.Product.GetAll);
        }
        public static DataTable GetAll(string storedProc)
        {
            return ExcuteProcedure(storedProc);
        }

        public static DataTable GetOne(string productId)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
						CreateParameter("@ProductID", SqlDbType.NVarChar, productId)
                    };
                return ExcuteProcedure(OrderConstants.StoreProcedure.Product.GetOne, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static Product LoadOne(string productId)
        {
            Product o = new Product();
            DataTable dt = GetOne(productId);
            if (dt.Rows.Count > 0)
            {
                //o.ID = int.Parse(dt.Rows[0][OrderConstants.Entities.Product.FieldName.ID].ToString());
                o.ProductID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.ProductID].ToString();
                o.ProductName = dt.Rows[0][OrderConstants.Entities.Product.FieldName.ProductName].ToString();
                o.EnglishName = dt.Rows[0][OrderConstants.Entities.Product.FieldName.EnglishName].ToString();
                o.VietName = dt.Rows[0][OrderConstants.Entities.Product.FieldName.VietName].ToString();
                o.JapanName = dt.Rows[0][OrderConstants.Entities.Product.FieldName.JapanName].ToString();
                o.Size = dt.Rows[0][OrderConstants.Entities.Product.FieldName.Size].ToString();
                o.MakerID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.MakerID].ToString();
                o.ProductGroupID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.ProductGroupID].ToString();
                o.SupplierID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.SupplierID].ToString();
                o.KindID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.KindID].ToString();
                o.UnitID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.UnitID].ToString();
                o.Price = decimal.Parse(dt.Rows[0][OrderConstants.Entities.Product.FieldName.Price].ToString());
                o.CurrencyID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.CurrencyID].ToString();
                o.PurposeID = dt.Rows[0][OrderConstants.Entities.Product.FieldName.PurposeID].ToString();

            }

            return o;
        }


        public static int Insert(Product o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID),
                        CreateParameter("@ProductName", SqlDbType.NVarChar, o.ProductName),
                        CreateParameter("@EnglishName", SqlDbType.NVarChar, o.EnglishName),
                        CreateParameter("@VietName", SqlDbType.NVarChar, o.VietName),
                        CreateParameter("@JapanName", SqlDbType.NVarChar, o.JapanName),
                        CreateParameter("@Size", SqlDbType.NVarChar, o.Size),
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@ProductGroupID", SqlDbType.NVarChar, o.ProductGroupID),
                        CreateParameter("@SupplierID", SqlDbType.NVarChar, o.SupplierID),
                        CreateParameter("@KindID", SqlDbType.NVarChar, o.KindID),
                        CreateParameter("@UnitID", SqlDbType.NVarChar, o.UnitID),
                        CreateParameter("@Price", SqlDbType.Decimal, o.Price),
                        CreateParameter("@CurrencyID", SqlDbType.NVarChar, o.CurrencyID),
                        CreateParameter("@PurposeID", SqlDbType.NVarChar, o.PurposeID),
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.Product.Insert, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static int Update(Product o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID),
                        CreateParameter("@ProductName", SqlDbType.NVarChar, o.ProductName),
                        CreateParameter("@EnglishName", SqlDbType.NVarChar, o.EnglishName),
                        CreateParameter("@VietName", SqlDbType.NVarChar, o.VietName),
                        CreateParameter("@JapanName", SqlDbType.NVarChar, o.JapanName),
                        CreateParameter("@Size", SqlDbType.NVarChar, o.Size),
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@ProductGroupID", SqlDbType.NVarChar, o.ProductGroupID),
                        CreateParameter("@SupplierID", SqlDbType.NVarChar, o.SupplierID),
                        CreateParameter("@KindID", SqlDbType.NVarChar, o.KindID),
                        CreateParameter("@UnitID", SqlDbType.NVarChar, o.UnitID),
                        CreateParameter("@Price", SqlDbType.Decimal, o.Price),
                        CreateParameter("@CurrencyID", SqlDbType.NVarChar, o.CurrencyID),
                        CreateParameter("@PurposeID", SqlDbType.NVarChar, o.PurposeID),
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.Product.Update, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static int Delete(Product o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID)
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.Product.Delete, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static DataTable GetTemplateTable()
        {
            DataTable table = new DataTable("243_Product");

            return table;
        }


        #endregion
    }
	
}
